home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / ZSI / twisted / client.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  9KB  |  279 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import time
  5. from zope.interface import classProvides, implements, Interface
  6. from twisted.web import client
  7. from twisted.internet import defer
  8. from twisted.internet import reactor
  9. from twisted.python import log
  10. from twisted.python.failure import Failure
  11. from ZSI.parse import ParsedSoap
  12. from ZSI.writer import SoapWriter
  13. from ZSI.fault import FaultFromFaultMessage
  14. from ZSI.wstools.Namespaces import WSA
  15. from WSresource import HandlerChainInterface, CheckInputArgs
  16.  
  17. class HTTPPageGetter(client.HTTPPageGetter):
  18.     
  19.     def handleStatus_500(self):
  20.         log.err('HTTP Error 500')
  21.  
  22.     
  23.     def handleStatus_404(self):
  24.         log.err('HTTP Error 404')
  25.  
  26.  
  27. client.HTTPClientFactory.protocol = HTTPPageGetter
  28.  
  29. def getPage(url, contextFactory = None, *args, **kwargs):
  30.     (scheme, host, port, path) = client._parse(url)
  31.     factory = client.HTTPClientFactory(url, *args, **kwargs)
  32.     if scheme == 'https':
  33.         if contextFactory is None:
  34.             raise RuntimeError, 'must provide a contextFactory'
  35.         
  36.         conn = reactor.connectSSL(host, port, factory, contextFactory)
  37.     else:
  38.         conn = reactor.connectTCP(host, port, factory)
  39.     return factory
  40.  
  41.  
  42. class ClientDataHandler:
  43.     classProvides(HandlerChainInterface)
  44.     readerClass = None
  45.     writerClass = None
  46.     
  47.     def processResponse(cls, soapdata, **kw):
  48.         if len(soapdata) == 0:
  49.             raise TypeError('Received empty response')
  50.         
  51.         ps = ParsedSoap(soapdata, readerclass = cls.readerClass)
  52.         if ps.IsAFault() is True:
  53.             log.msg('Received SOAP:Fault', debug = True)
  54.             raise FaultFromFaultMessage(ps)
  55.         
  56.         return ps
  57.  
  58.     processResponse = classmethod(processResponse)
  59.     
  60.     def processRequest(cls, obj, nsdict = { }, header = True, **kw):
  61.         tc = None
  62.         if kw.has_key('requesttypecode'):
  63.             tc = kw['requesttypecode']
  64.         elif kw.has_key('requestclass'):
  65.             tc = kw['requestclass'].typecode
  66.         else:
  67.             tc = getattr(obj.__class__, 'typecode', None)
  68.         sw = SoapWriter(nsdict = nsdict, header = header, outputclass = cls.writerClass)
  69.         sw.serialize(obj, tc)
  70.         return sw
  71.  
  72.     processRequest = classmethod(processRequest)
  73.  
  74.  
  75. class WSAddressHandler:
  76.     implements(HandlerChainInterface)
  77.     uri = WSA.ADDRESS
  78.     
  79.     def processResponse(self, ps, wsaction = None, soapaction = None, **kw):
  80.         addr = self.address
  81.         addr.parse(ps)
  82.         action = addr.getAction()
  83.         if not action:
  84.             raise WSActionException('No WS-Action specified in Request')
  85.         
  86.         if not soapaction:
  87.             return ps
  88.         
  89.         soapaction = soapaction.strip('\'"')
  90.         if soapaction and soapaction != wsaction:
  91.             raise WSActionException('SOAP Action("%s") must match WS-Action("%s") if specified.' % (soapaction, wsaction))
  92.         
  93.         return ps
  94.  
  95.     
  96.     def processRequest(self, sw, wsaction = None, url = None, endPointReference = None, **kw):
  97.         Address = Address
  98.         import ZSI.address
  99.         if sw is None:
  100.             self.address = None
  101.             return None
  102.         
  103.         if not sw.header:
  104.             raise RuntimeError, 'expecting SOAP:Header'
  105.         
  106.         self.address = addr = Address(url, wsAddressURI = self.uri)
  107.         addr.setRequest(endPointReference, wsaction)
  108.         addr.serialize(sw, typed = False)
  109.         return sw
  110.  
  111.  
  112.  
  113. class DefaultClientHandlerChain:
  114.     
  115.     def __init__(self, *handlers):
  116.         self.handlers = handlers
  117.         self.debug = len(log.theLogPublisher.observers) > 0
  118.         self.flow = None
  119.  
  120.     __init__ = CheckInputArgs(HandlerChainInterface)(__init__)
  121.     
  122.     def parseResponse(ps, replytype):
  123.         return ps.Parse(replytype)
  124.  
  125.     parseResponse = staticmethod(parseResponse)
  126.     
  127.     def processResponse(self, arg, replytype, **kw):
  128.         if self.debug:
  129.             log.msg('--->PROCESS REQUEST\n%s' % arg, debug = 1)
  130.         
  131.         for h in self.handlers:
  132.             arg.addCallback(h.processResponse, **kw)
  133.         
  134.         arg.addCallback(self.parseResponse, replytype)
  135.  
  136.     
  137.     def processRequest(self, arg, **kw):
  138.         if self.debug:
  139.             log.msg('===>PROCESS RESPONSE: %s' % str(arg), debug = 1)
  140.         
  141.         if arg is None:
  142.             return None
  143.         
  144.         for h in self.handlers:
  145.             arg = h.processRequest(arg, **kw)
  146.         
  147.         s = str(arg)
  148.         if self.debug:
  149.             log.msg(s, debug = 1)
  150.         
  151.         return s
  152.  
  153.  
  154.  
  155. class DefaultClientHandlerChainFactory:
  156.     protocol = DefaultClientHandlerChain
  157.     
  158.     def newInstance(cls):
  159.         return cls.protocol(ClientDataHandler)
  160.  
  161.     newInstance = classmethod(newInstance)
  162.  
  163.  
  164. class WSAddressClientHandlerChainFactory:
  165.     protocol = DefaultClientHandlerChain
  166.     
  167.     def newInstance(cls):
  168.         return cls.protocol(ClientDataHandler, WSAddressHandler())
  169.  
  170.     newInstance = classmethod(newInstance)
  171.  
  172.  
  173. class Binding:
  174.     agent = 'ZSI.twisted client'
  175.     factory = DefaultClientHandlerChainFactory
  176.     defer = False
  177.     
  178.     def __init__(self, url = None, nsdict = None, contextFactory = None, tracefile = None, **kw):
  179.         self.url = url
  180.         if not nsdict:
  181.             pass
  182.         self.nsdict = { }
  183.         self.contextFactory = contextFactory
  184.         self.http_headers = {
  185.             'content-type': 'text/xml' }
  186.         self.trace = tracefile
  187.  
  188.     
  189.     def addHTTPHeader(self, key, value):
  190.         self.http_headers[key] = value
  191.  
  192.     
  193.     def getHTTPHeaders(self):
  194.         return self.http_headers
  195.  
  196.     
  197.     def Send(self, url, opname, pyobj, nsdict = { }, soapaction = None, chain = None, **kw):
  198.         if not url:
  199.             pass
  200.         url = self.url
  201.         cookies = None
  202.         if chain is not None:
  203.             cookies = chain.flow.cookies
  204.         
  205.         d = { }
  206.         d.update(self.nsdict)
  207.         d.update(nsdict)
  208.         if soapaction is not None:
  209.             self.addHTTPHeader('SOAPAction', soapaction)
  210.         
  211.         chain = self.factory.newInstance()
  212.         soapdata = chain.processRequest(pyobj, nsdict = nsdict, soapaction = soapaction, **kw)
  213.         if self.trace:
  214.             print >>self.trace, '_' * 33, time.ctime(time.time()), 'REQUEST:'
  215.             print >>self.trace, soapdata
  216.         
  217.         f = getPage(str(url), contextFactory = self.contextFactory, postdata = soapdata, agent = self.agent, method = 'POST', headers = self.getHTTPHeaders(), cookies = cookies)
  218.         if isinstance(f, Failure):
  219.             return f
  220.         
  221.         chain.flow = f
  222.         self.chain = chain
  223.         return chain
  224.  
  225.     
  226.     def Receive(self, replytype, chain = None, **kw):
  227.         if not chain:
  228.             pass
  229.         chain = self.chain
  230.         d = chain.flow.deferred
  231.         if self.trace:
  232.             
  233.             def trace(soapdata):
  234.                 print >>self.trace, '_' * 33, time.ctime(time.time()), 'RESPONSE:'
  235.                 print >>self.trace, soapdata
  236.                 return soapdata
  237.  
  238.             d.addCallback(trace)
  239.         
  240.         chain.processResponse(d, replytype, **kw)
  241.         if self.defer:
  242.             return d
  243.         
  244.         failure = []
  245.         append = failure.append
  246.         
  247.         def errback(result):
  248.             append(result)
  249.  
  250.         d.addErrback(errback)
  251.         while not d.called:
  252.             reactor.runUntilCurrent()
  253.             t2 = reactor.timeout()
  254.             if reactor.running:
  255.                 pass
  256.             t = t2
  257.             reactor.doIteration(t)
  258.             continue
  259.             (None,)
  260.         pyobj = d.result
  261.         if len(failure):
  262.             failure[0].raiseException()
  263.         
  264.         return pyobj
  265.  
  266.  
  267.  
  268. def trace():
  269.     if trace:
  270.         print >>trace, '_' * 33, time.ctime(time.time()), 'RESPONSE:'
  271.         for i in (self.reply_code, self.reply_msg):
  272.             print >>trace, str(i)
  273.         
  274.         print >>trace, '-------'
  275.         print >>trace, str(self.reply_headers)
  276.         print >>trace, self.data
  277.     
  278.  
  279.